from django.contrib.auth.decorators import login_required
from django.http import Http404
from django.shortcuts import render, redirect, reverse
from django.views.decorators.cache import cache_page
from django.views.decorators.http import require_GET
from django.contrib.auth import authenticate, login, logout, get_user_model
from django.views.decorators.vary import vary_on_headers
from rest_framework.authtoken.models import Token
from rest_framework.views import APIView

from utils.context import get_context
from utils.response import APIResponse, Code


@require_GET
def index(request):
    context = get_context()
    return render(request, 'index.html', context=context)


@require_GET
def detail(request, pid):
    if pid:
        context = get_context()
        context['pid'] = pid
        return render(request, 'detail.html', context=context)
    raise Http404


@require_GET
def types(request):
    context = get_context()
    return render(request, 'types.html', context=context)


@cache_page(5 * 60)
@require_GET
def topics(request):
    context = get_context()
    return render(request, 'topics.html', context=context)


@require_GET
def search(request):
    context = get_context()
    return render(request, 'search.html', context=context)


class LoginView(APIView):
    def get(self, request):
        return render(request, 'login.html')

    def post(self, request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request, user)
            old_token = Token.objects.filter(user=user)
            old_token.delete()
            # 创建新的Token
            token = Token.objects.create(user=user)  # djangorestframework Token
            return APIResponse(msg='登录成功~~', data={'token': token.key})
        else:
            return APIResponse(code=Code.BadParams, msg='账号或密码错误~~')


def logout_view(request):
    Token.objects.filter(user=request.user).delete()
    logout(request)
    return redirect(reverse("front:login"))


class RegisterView(APIView):
    def post(self, request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        if get_user_model().objects.filter(username=username).exists():
            return APIResponse(code=Code.BadParams, msg='该账号已存在~~')
        else:
            user = get_user_model()(username=username)
            user.set_password(password)
            user.save()
            login(request, user)

            login(request, user)
            old_token = Token.objects.filter(user=user)
            old_token.delete()
            # 创建新的Token
            token = Token.objects.create(user=user)  # djangorestframework Token
            return APIResponse(msg='注册成功~~', data={'token': token.key})


@require_GET
@login_required
def publish(request):
    context = get_context()
    return render(request, 'publish.html', context=context)


@require_GET
@login_required
def front_cms(request):
    return render(request, 'cms.html')
